package com.example.backend.service.impl;

import com.example.backend.dao.UserRepository;
import com.example.backend.pojo.User;
import com.example.backend.response.LoginResponse;
import com.example.backend.service.UserService;
import com.example.backend.util.JwtUtils;
import com.example.backend.util.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserRepository userRepository;

    @Override
    public Response<LoginResponse> login(String username, String password) {
        User user = userRepository.findUserByUsernameAndPassword(username, password);
        if (user == null) {
            return new Response<>(Response.FAIL, "verify failed", null);
        } else {
            Boolean haveModel = false;
            Map<String, String> payload = new HashMap<>();
            payload.put("id", user.getId().toString());
            payload.put("username", user.getUsername());
            String token = JwtUtils.getToken(payload, user.getPassword());
            if (user.getModel() != null) {
                haveModel = true;
            }
            LoginResponse loginResponse = new LoginResponse(token, haveModel, user.getModel(), user.getColor(), user.getId());
            return new Response<LoginResponse>(Response.SUCCESS, "success", loginResponse);
        }
    }

    @Override
    public Response<User> create(User user) {
        User sameUsernameUser = userRepository.findUserByUsername(user.getUsername());
        if (sameUsernameUser != null) {
            return new Response<>(Response.FAIL, "username exist", null);
        } else {
            userRepository.save(user);
            return new Response<>(Response.SUCCESS, "success", null);
        }
    }

    @Override
    public Response<Object> exist(String username) {
        User user = userRepository.findUserByUsername(username);
        UserService.Exist exist = new UserService.Exist();
        if (user == null) {
            exist.setExist(0);
            return new Response<>(Response.SUCCESS, "not exist", exist);
        } else {
            exist.setExist(1);
            return new Response<>(Response.SUCCESS, "exist", exist);
        }
    }

    @Override
    public Response<Object> update(User user) {
        User sameIdUser = userRepository.findUserById(user.getId());
        if (sameIdUser == null) {
            return new Response<>(Response.FAIL, "该用户不存在！", null);
        } else {
            sameIdUser.setUsername(user.getUsername());
            sameIdUser.setPassword(user.getPassword());
            userRepository.save(sameIdUser);
            Map<String, String> payload = new HashMap<>();
            payload.put("id", user.getId().toString());
            payload.put("username", user.getUsername());
            String token = JwtUtils.getToken(payload, user.getPassword());
            Boolean haveModel = false;
            if (user.getModel() != null) {
                haveModel = true;
            }
            LoginResponse loginResponse = new LoginResponse(token, haveModel, sameIdUser.getModel(), sameIdUser.getColor(), user.getId());
            return new Response<>(Response.SUCCESS, "用户信息修改成功！", loginResponse);
        }
    }

    @Override
    public Response<Object> updateModel(User user) {
        User sameNameUser = userRepository.findUserByUsername(user.getUsername());
        if (sameNameUser == null) {
            return new Response<>(Response.FAIL, "该用户不存在！", null);
        } else {
            sameNameUser.setModel(user.getModel());
            sameNameUser.setColor(user.getColor());
            userRepository.save(sameNameUser);
            return new Response<>(Response.SUCCESS, "用户模型修改成功！", null);
        }
    }

    @Override
    public Response<User> findUserById(Integer id) {
        User user = userRepository.findUserById(id);
        if (user == null) {
            return new Response<>(Response.FAIL, "no such id", null);
        } else {
            return new Response<>(Response.SUCCESS, "success", user);
        }
    }
}
